package cn.wzptsoft.tinymallbe.service;

import cn.wzptsoft.tinymallbe.entity.Category;
import cn.wzptsoft.tinymallbe.repository.CategoryRepository;
import cn.wzptsoft.tinymallbe.vo.CategoryVo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    final
    CategoryRepository categoryRepository;

    public CategoryService(CategoryRepository categoryRepository) {
        this.categoryRepository = categoryRepository;
    }

    /**
     * 查询所有商品分类
     */
    public CategoryVo getAll() {
        List<Category> roots = categoryRepository.findAllByIsRootOrderBySortAsc(true);
        List<Category> subs = categoryRepository.findAllByIsRootOrderBySortAsc(false);
        CategoryVo result = new CategoryVo();
        result.setRoots(roots);
        result.setSubs(subs);
        return result;
    }

    /**
     * 查找根分类 用于宫格展示
     */
    public List<Category> getRootCategory() {
        return categoryRepository.findAllByIsRootOrderBySortAsc(true);
    }
}
